<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="qqInfo.avatar" alt="" />
      <p>
        Hi，{{ qqInfo.nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~
      </p>
    </div>
    <Form
      ref="target"
      autocomplete="off"
      :validationSchema="rules"
      v-slot="{ errors }"
    >
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            name="mobile"
            v-model="form.mobile"
            class="input"
            type="text"
            placeholder="绑定的手机号"
          />
        </div>
        <div v-if="errors.mobile" class="error">{{ errors.mobile }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            name="code"
            v-model="form.code"
            class="input"
            type="text"
            placeholder="短信验证码"
          />
          <span class="code" @click="send">
            {{ time === 0 ? '发送验证码' : `${time}s后发送` }}
          </span>
        </div>
        <div v-if="errors.code" class="error">{{ errors.code }}</div>
      </div>
      <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
    </Form>
  </div>
</template>

<script>
import { reactive, ref } from 'vue'
import { Message } from '@/components'
import { Form, Field } from 'vee-validate'
import { mobile, code } from '@/utils/validate'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { useCounter } from '@/hooks'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const QC = window.QC
export default {
  name: 'CallbackBind',
  components: {
    Form,
    Field
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const qqInfo = reactive({
      nickname: '',
      avatar: ''
    })
    const form = reactive({
      mobile: '',
      code: ''
    })
    const rules = {
      mobile,
      code
    }
    const router = useRouter()
    const store = useStore()
    // 1. 判断qq是否登录
    if (QC.Login.check()) {
      console.log(1)
      // 2. 获取QQ的用户信息
      // QC.api('get_user_info')
      QC.api('get_user_info')
        .success(res => {
          console.log(3)
          console.log('res', res)
          qqInfo.nickname = res.data.nickname
          qqInfo.avatar = res.data.figureurl_qq_2
        })
        .error(() => {
          Message({ type: 'error', text: '获取QQ信息失败，请重试' })
        })
    }

    const target = ref(null)
    const { time, start } = useCounter(10)
    const send = async () => {
      if (time.value > 0) return
      // 1. 校验手机号合法性
      const valid = mobile(form.mobile)
      if (valid !== true) {
        return target.value.setFieldError('mobile', valid)
      }
      // 2. 发送请求，获取验证码
      await userQQBindCode(form.mobile)
      // 3. 倒计时功能
      start()
    }

    const bind = async () => {
      // 1. 整体校验
      const valid = await target.value.validate()
      if (!valid) return
      // 2. 发送请求，绑定QQ
      const res = await userQQBindLogin({
        mobile: form.mobile,
        code: form.code,
        unionId: props.unionId
      })
      // 跳转到首页
      Message({ text: '登录成功' })
      store.commit('user/setProfile', res.result)
      router.push('/')
    }
    return {
      qqInfo,
      form,
      rules,
      send,
      target,
      time,
      bind
    }
  }
}
</script>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
